<?php


namespace app\zwt\controller;


use app\zwt\model\GoodsModel;
use app\zwt\validate\GoodsValidate;
use cmf\controller\AdminBaseController;
use think\facade\Db;
use think\Request;

class GoodsController extends AdminBaseController
{
    //显示首页
    public function index(){
        //获取查询数据
        $name = input('name');
        $where = [];
        //判断是否有值
        if($name){
            $where[] = ['name','like','%'.$name.'%'];
        }
        //查询排序分页
        $list = GoodsModel::order('id DESC')
            ->where($where)
            ->paginate(10);
        return $this->fetch('index',[
            'list' => $list,
        ]);
    }
    //添加
    public function add(){
        $list = GoodsModel::select();
        return $this->fetch('add',[
            'list' => $list,
        ]);
    }
    public function addPost(){
        $data = input('post.');
        $data['photos'] = implode(',', $data['photos']);
        $validate = new GoodsValidate();
        $result = $validate->scene('add')->check($data);
        if(true !== $result){
            $this->error($validate->getError());
        }
        $image = $data['image'];
        $url = cmf_get_image_url($image);
        $name = $data['name'];
        $photos = $data['photos'];
        $title = $data['title'];
        $status = $data['status'];
        $zy = $data['zy'];
        $yf = $data['yf'];
        $url1 = $data['url'];
        $data['create_time'] = date("Y-m-d H:i:s");

        //入库
        if(GoodsModel::create([
            'name' => $name,
            'image' => $url,
            'photos' => $photos,
            'title' => $title,
            'status' => $status,
            'zy' => $zy,
            'yf' => $yf,
            'url' => $url1,
            'create_time' => $data['create_time']

        ])){
            $this->success('添加成功','Goods/index');
        }else{
            $this->error('添加失败');
        }
    }
    //修改
    public function edit(){
        $id = input('id');
        $list = GoodsModel::find($id);
        return $this->fetch('edit',[
            'list' => $list,
        ]);
    }
    public function editPost(){
        $data = input('post.');
        //验证器
        $validate = new GoodsValidate();
        $result = $validate->scene('edit')->check($data);
        if(!$result){
            $this->error($validate->getError());
        }
        //查询是否有此记录
        $list = GoodsModel::find($data['id']);
        if(!$list){
            $this->error('未查询到该记录');
        }
        if($list->update($data)){
            $this->success('修改成功','Goods/index');
        }else{
            $this->error('修改失败');
        }
    }
    //删除
    public function delete(){
        $id = input('id/d','');
        $del = GoodsModel::destroy($id);
        if($del){
            $this->success('删除成功','Goods/index');
        }else{
            $this->error('删除失败');
        }
    }
    //显示隐藏内容
    public function isShow(Request $request){
        $id = $request->param('id');
        if(!$id){
            $this->error('未查询到该id');
        }
        $goods = GoodsModel::find($id);
        $zy = null;
        if($goods['zy'] == 1){
            $zy = 0;
        }
        if($goods['zy'] == 0){
            $zy = 1;
        }
        if(GoodsModel::update(['zy'=>$zy],['id'=>$id])){
            $this->success('修改成功','Goods/index');
        }else{
            $this->error('修改失败');
        }
    }
//自营
//    public function isShow1(Request $request){
//        $id = $request->param('id');
//        if(!$id){
//            $this->error('未查询到该id');
//        }
//        $goods = GoodsModel::find($id);
//        $zy = null;
//        if($goods['zy'] == 1){
//            $zy = 1;
//        }
//        if($goods['zy'] == 0){
//            $zy = 0;
//        }
//        if(GoodsModel::update(['zy'=>$zy],['id'=>$id])){
//            $this->success('修改成功','Goods/index');
//        }else{
//            $this->error('修改失败');
//        }
//    }

//打开配置页面
    public function setGoodsAttr()
    {
        $goodsId = input('goods_id');
        $this->assign('goods_id',$goodsId);
        return $this->fetch('add_goods_attr');
    }

    //配置提交
    public function setGoodsAttrPost()
    {
        $attrs = input('post.attr');
        $goodsId = input('post.goods_id');
        if(count($attrs) < 1 ){
            $this->error('请至少配置一个属性! ');
        }
        $insert_data = [];
        foreach ($attrs as $attr) {
            $insert_data[] = [
                'goods_id' => $goodsId,
                'name' => $attr
            ];
        }
        Db::name('goods_attr')->insertAll($insert_data);
        $this->success('保存成功');
    }

    //打开配置商品属性值页面
    public function setGoodsAttrValue()
    {
        $goodsId = input('goods_id');
        $this->assign('goods_id',$goodsId);
        return $this->fetch('add_goods_attr_value');
    }

    //查询某个商品有哪些属性
    public function getGoodsAttr()
    {
        $goodsId = input('goods_id');
        $attrs = Db::name('goods_attr')
            ->where('goods_id',$goodsId)
            ->select();
        return json([
            'code' => 1,
            'msg' => '商品属性',
            'data' => $attrs
        ]);
    }

    //提交表单
    public function setGoodsAttrValuePost()
    {
        halt(input());
    }
}